<?php $this->load->view('admin/Public/header'); ?>
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">分组名称</label>
					<div class="layui-input-inline">
						<input type="text" id="group_key" placeholder="请输入分组名称" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">配置key</label>
					<div class="layui-input-inline">
						<input type="text" id="key" placeholder="请输入配置key" autocomplete="off" class="layui-input">
					</div>
				</div>
				<button class="layui-btn" data-type="reload">搜索</button>
			</div>
		</div>

		<div class="layui-card-body">
			<script type="text/html" id="toolbarDemo">
				<div class="layui-btn-container">
					<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
				</div>
			</script>
			<table id="LAY-app-content-list" lay-filter="LAY-app-content-list" ></table>
			<script type="text/html" id="barDemo">
				<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
			</script>
		</div>
	</div>
</div>
<script>
    layui.config({
        base: '/public/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table'], function(){
        var table = layui.table
            ,form = layui.form;

        table.render({
            elem: '#LAY-app-content-list'
            , url: '/admin/Setting/set_list'
            , title: '系统配置'
            ,toolbar: '#toolbarDemo'
            , totalRow: true
            , page: true
            ,cols: [[
                {field:'id', title:'ID',width:80,  fixed: 'left', unresize: true, sort: true}
                ,{field:'group_key', title:'分组名称'}
                ,{field:'key', title:'配置key'}
                ,{field:'name', title:'配置名称'}
                ,{field:'value', title:'配置值'}
                ,{field:'inx', title:'排序'}
                ,{field:'addtime', title:'创建时间'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
        });

        var $ = layui.$, active = {
            reload: function () {
                var group_key = $('#group_key').val();
                var key = $('#key').val();
                //执行重载
                table.reload('LAY-app-content-list', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        group_key: group_key,
                        key: key
                    }
                });
            }
        };

        //工具栏事件
        table.on('toolbar(LAY-app-content-list)', function(obj){
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2
                        ,title: '添加配置'
                        ,content: '/admin/Setting/set_add'
                        ,maxmin: true
                        ,area: ['550px', '550px']
                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(LAY-app-content-list)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑配置'
                    ,content: '/admin/Setting/set_edit?id='+data.id
                    ,maxmin: true
                    ,area: ['550px', '550px']
                });
            }
            if(obj.event === 'del'){
				layer.confirm('确定要删除数据吗？', function(index) {
					//发异步删除数据
					$.ajax({
						url: '/admin/Setting/set_del',
						type: 'post',
						dataType: 'json',
						data: {
							id: data.id
						},
						success: function(data, status) {
                            if(data.code==1)
                            {
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.close(index);
                                layer.msg(data.content, {
                                    icon: 1,
                                    time: 1000
                                });
                            }
                            else
                            {
                                layer.msg(data.content, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
						},
						fail: function(err, status) {
							// console.log(err);
						}
					});
				});
            }
        });
        $('.layui-card .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });
</script>
</body>
</html>
